package cartoon.room.read.room;

import android.content.Context;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider;

import java.util.List;


import cartoon.room.CartoonHistoryDataBase;
import cartoon.room.CartoonHistoryModel;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public  class CartoonReadRxJava {
    private static volatile CartoonReadRxJava INSTANCE;
    private static Context context;
    private static CarttonReadHistoryDao historyDao;
    private static ViewModelProvider viewModel;


    private CartoonReadRxJava() {
    }

    public static CartoonReadRxJava getInstance(Context context) {
        if (INSTANCE == null) {
            synchronized (CartoonReadRxJava.class) {
                if (INSTANCE == null) {
                    CartoonReadRxJava.context = context;
                    INSTANCE = new CartoonReadRxJava();

                    cartoon.room.read.room.CartoonReadHistoryDataBase database = CartoonReadHistoryDataBase.getInstance(context);
                    historyDao = database.getStudentDao();


                }
            }
        }
        return INSTANCE;
    }


    public void addHistoryDao(cartoon.room.read.room.CartoonReadHistoryModel historyModel) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("A");

            }
        })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(Schedulers.computation())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        historyDao.addHistoryDao(historyModel);

                    }
                });
    }


    public void addsHistoryDao(cartoon.room.read.room.CartoonReadHistoryModel... historyModel) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("A");

            }
        })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(Schedulers.computation())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        historyDao.addsHistoryDao(historyModel);
                    }
                });

    }

    public void deleteHistory(cartoon.room.read.room.CartoonReadHistoryModel... historyModel) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("A");

            }
        })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(Schedulers.computation())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        historyDao.deleteHistory(historyModel);
                    }
                });

    }


    public void updateHistory(cartoon.room.read.room.CartoonReadHistoryModel... historyModel) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("A");

            }
        })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(Schedulers.computation())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        historyDao.updateHistory(historyModel);
                    }
                });

    }

    public void deleteTableData() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("A");

            }
        })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(Schedulers.computation())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        historyDao.deleteTableData();

                    }
                });

    }


    public LiveData<List<CartoonReadHistoryModel>> getAllSLive() {

        return historyDao.getAllSLive();
    }
}
